using System.Reflection;
using DataAccess.Api.Extensions;
using Dapper.MySql;
using Dapper.Extensions;
using Dapper.Extensions.Configuration;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
	options.CustomSchemaIds(type => type.FullName);
});

builder.Services.AddDapper(options => options
	.UseMySql("Server=localhost;Database=ormapping;Port=3306;User=root;Password=123456;")
	.EnableLogging(logConfig =>
	{
		logConfig.WithLogLevel(LogLevel.Information)
			.WithSensitiveDataLogging() // 开发环境
			.WithQueryMessage("Dapper SQL: {query}\n 参数: {params}\n 耗时: {elapsed}ms")
			.WithConnectionProjector(conn => new
			{
				conn.Database,
				conn.DataSource,
				conn.State
			});
	})
	.EnabledRetry(retry =>
	{
		retry.RetryCount = 3;
		retry.RetryDelayMilliseconds = 1000;
	}));

builder.Services.AddEndpoints(Assembly.GetExecutingAssembly());
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapEndpoints();
await app.RunAsync();
